﻿using Application.Common.Interfaces;
using FluentValidation;
using Microsoft.EntityFrameworkCore;

namespace Application.Features.Organizations.Commands.Delete;

/// <summary>
/// 校验删除组织机构信息
/// </summary>
public class DeleteOrganizationCommandValidator : AbstractValidator<DeleteOrganizationCommand>
{
    private readonly IApplicationDbContext _context;
    public DeleteOrganizationCommandValidator(IApplicationDbContext context)
    {
        _context = context;

        RuleFor(x => x.OrganizationId).MustAsync(OrganizationExistsAsync).WithMessage("非法数据！");
    }

    private async Task<bool> OrganizationExistsAsync(long organizationId, CancellationToken cancellationToken)
    {
        return await _context.Organizations.AnyAsync(x => x.Id == organizationId);
    }
}